//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "PiecewiseConstantCorrelation.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::CPiecewiseConstantCorrelation (boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPiecewiseConstantCorrelation = NULL;
#endif
	_ppPiecewiseConstantCorrelation = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::CPiecewiseConstantCorrelation (QuantLib::PiecewiseConstantCorrelation& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPiecewiseConstantCorrelation = NULL;
#endif
	_ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (&childNative);
    
    _PiecewiseConstantCorrelationOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::CPiecewiseConstantCorrelation (CPiecewiseConstantCorrelation^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPiecewiseConstantCorrelation = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::CPiecewiseConstantCorrelation (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPiecewiseConstantCorrelation = NULL;
#endif
	if (!t->IsSubclassOf(CPiecewiseConstantCorrelation::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::CPiecewiseConstantCorrelation (QuantLib::Handle<QuantLib::PiecewiseConstantCorrelation>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPiecewiseConstantCorrelation = &childNative;
	_ppPiecewiseConstantCorrelation = &static_cast<boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>>(childNative.currentLink());
    
    _PiecewiseConstantCorrelationOwner = owner;
}
Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::CPiecewiseConstantCorrelation (QuantLib::Handle<QuantLib::PiecewiseConstantCorrelation> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPiecewiseConstantCorrelation = &childNative;
	_ppPiecewiseConstantCorrelation = &static_cast<boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::CPiecewiseConstantCorrelation (QuantLib::PiecewiseConstantCorrelation childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPiecewiseConstantCorrelation = NULL;
#endif
	_ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (new QuantLib::PiecewiseConstantCorrelation (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::~CPiecewiseConstantCorrelation ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPiecewiseConstantCorrelation != NULL)
    {
	    delete _ppPiecewiseConstantCorrelation;
        _ppPiecewiseConstantCorrelation = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::!CPiecewiseConstantCorrelation ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPiecewiseConstantCorrelation != NULL)
    {
	    delete _ppPiecewiseConstantCorrelation;
    }
}
QuantLib::PiecewiseConstantCorrelation& Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::GetReference ()
{
    if (_ppPiecewiseConstantCorrelation == NULL) throw REFNEW NativeNullException ();
	return **_ppPiecewiseConstantCorrelation;
}
boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>& Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::GetShared ()
{
    if (_ppPiecewiseConstantCorrelation == NULL) throw REFNEW NativeNullException ();
	return *_ppPiecewiseConstantCorrelation;
}
QuantLib::PiecewiseConstantCorrelation* Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::GetPointer ()
{
    if (_ppPiecewiseConstantCorrelation == NULL) throw REFNEW NativeNullException ();
	return &**_ppPiecewiseConstantCorrelation;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PiecewiseConstantCorrelation>& Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::GetHandle ()
{
	if (_phPiecewiseConstantCorrelation == NULL)
	{
		_phPiecewiseConstantCorrelation = new Handle<QuantLib::PiecewiseConstantCorrelation> (*_ppPiecewiseConstantCorrelation);
	}
	return *_phPiecewiseConstantCorrelation;
}
#endif
bool Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::HasNative () 
{
	return (_ppPiecewiseConstantCorrelation != NULL);
}

Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::Correlation (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppPiecewiseConstantCorrelation)->correlation ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = REFNEW Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::Correlations::get ()
{
    try
    {
    	std::vector<QuantLib::Matrix>& _rv = (std::vector<QuantLib::Matrix>&)(*_ppPiecewiseConstantCorrelation)->correlations ( );   
        CoVector<Cephei::QL::Math::IMatrix^>^ _nrv = REFNEW CoVector<Cephei::QL::Math::IMatrix^>(REFNEW CMatrixVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppPiecewiseConstantCorrelation)->numberOfRates ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::RateTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppPiecewiseConstantCorrelation)->rateTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CPiecewiseConstantCorrelation::Times::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppPiecewiseConstantCorrelation)->times ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

